package gw.com.android.ui.me;

import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import gw.com.android.app.GTConfig;
import gw.com.android.model.ConfigSettingDeal;
import gw.com.android.model.ConfigType;
import gw.com.android.presenter.PresenterImpl;
import www.com.library.model.DataItemDetail;
import www.com.library.model.DataItemResult;
import www.com.library.util.DeviceUtil;

/**
 * @author jett
 * @since 2018-11-29.
 */
public class MineAdapter extends RecyclerView.Adapter {

    private final int TYPE_HEADER = 1;
    private final int TYPE_FOOTER = 2;

    private FragmentActivity mContext;
    private PresenterImpl presenterImpl;
    private DataItemResult mDatas;

    private View headerView;
    private View footerView;
    private int unReadCount = 0;

    MineAdapter(FragmentActivity context, PresenterImpl presenterImpl) {
        mContext = context;
        this.presenterImpl = presenterImpl;
    }

    void setDatas(DataItemResult datas) {
        this.mDatas = datas;
    }

    void setHeaderView(View headerView) {
        this.headerView = headerView;
    }

    void setFooterView(View footerView) {
        this.footerView = footerView;
    }

    void setUnReadCount(int unReadCount) {
        this.unReadCount = unReadCount;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            return new HeaderHolder(headerView);
        } else if (viewType == TYPE_FOOTER) {
            return new HeaderHolder(footerView);
        }
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_icon_arrow, parent, false);
        return new MineHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MineHolder) {
            DataItemDetail item = getItem(position);
            if (item == null) {
                return;
            }
            MineHolder vh = (MineHolder) holder;
            vh.data = item;
            vh.mContent.setText(item.getString(ConfigType.CONFIG_TYPE_TITLE_TAG));
            vh.redPoint.setVisibility(View.INVISIBLE);
            vh.redCount.setVisibility(View.INVISIBLE);
            vh.subTitle.setVisibility(View.INVISIBLE);
            String key = item.getString(ConfigType.CONFIG_TYPE_KEY_TAG);
            if (key.equals(ConfigType.BULLENTIN_TYPE_TAG)) {
                int count = unReadCount;
                if (count > 0) {
                    vh.redCount.setVisibility(View.VISIBLE);
                    if (count > 99) {
                        vh.redCount.setText("99+");
                    } else {
                        vh.redCount.setText(String.valueOf(count));
                    }
                } else {
                    vh.redCount.setVisibility(View.GONE);
                }
            } else if (key.equals(ConfigType.SWITCH_REAL_TAG)) {
                vh.subTitle.setVisibility(View.VISIBLE);
                vh.subTitle.setText(R.string.me_switch_real_subtitle);
                vh.subTitle.setTextColor(0xFFA6A6A6);
                Drawable drawable = mContext.getResources().getDrawable(R.mipmap.a_me_hot);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                vh.mContent.setCompoundDrawablePadding(DeviceUtil.instance().dip2px(2, mContext));
                vh.mContent.setCompoundDrawables(drawable, null, null, null);
                vh.mContent.setPadding(DeviceUtil.instance().dip2px(10, mContext), 0, 0, 0);
            } else if (key.equals(ConfigType.RECOMMENDED_TAG)) {
                vh.subTitle.setVisibility(View.VISIBLE);
                vh.subTitle.setText(R.string.me_invite_desc);
                vh.subTitle.setTextColor(0xFFFC9A4B);
            } else {
                vh.subTitle.setVisibility(View.GONE);
            }
            if (key.equals("newNotice")) {
                if (!GTConfig.instance().isNewNoticeClicked()) {
                    vh.redPoint.setVisibility(View.VISIBLE);
                }
            } else if (key.equals(ConfigType.SYSTEM_SETTING_TAG)) {
                if (GTConfig.instance().hasNewVersion) {
                    vh.redPoint.setVisibility(View.VISIBLE);
                } else {
                    vh.redPoint.setVisibility(View.GONE);
                }
            }
            vh.divider.setVisibility(View.VISIBLE);
        }
    }

    private DataItemDetail getItem(int position) {
        if (headerView != null) {
            position--;
        }
        if (position >= 0 && position < mDatas.getDataCount()) {
            return mDatas.getItem(position);
        }
        return null;
    }

    @Override
    public int getItemCount() {
        int addtional = 0;
        if (headerView != null) {
            addtional++;
        }
        if (footerView != null) {
            addtional++;
        }
        if (mDatas != null) {
            return mDatas.getDataCount() + addtional;
        }
        return addtional;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 && headerView != null) {
            return TYPE_HEADER;
        }
        if (position == getItemCount() - 1 && footerView != null) {
            return TYPE_FOOTER;
        }
        return super.getItemViewType(position);
    }

    private class MineHolder extends RecyclerView.ViewHolder {
        TextView mContent;
        View redPoint;
        TextView redCount;
        TextView subTitle;
        View divider;
        DataItemDetail data;

        MineHolder(View itemView) {
            super(itemView);
            mContent = itemView.findViewById(R.id.item_title);
            redPoint = itemView.findViewById(R.id.item_redpoint);
            redCount = itemView.findViewById(R.id.item_redcount);
            subTitle = itemView.findViewById(R.id.item_subtitle);
            divider = itemView.findViewById(R.id.item_divider);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new ConfigSettingDeal().onViewClick(mContext, presenterImpl, data, "", null);
                }
            });
        }

    }

    private class HeaderHolder extends RecyclerView.ViewHolder {
        HeaderHolder(View itemView) {
            super(itemView);
        }
    }

}
